package org.csu.webjpetstoreback.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.mapper.LineItemsMapper;
import org.csu.webjpetstoreback.mapper.OrdersMapper;
import org.csu.webjpetstoreback.model.Item;
import org.csu.webjpetstoreback.model.LineItem;
import org.csu.webjpetstoreback.model.Orders;
import org.csu.webjpetstoreback.service.LineItemService;
import org.csu.webjpetstoreback.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LineItemServiceImpl extends ServiceImpl<LineItemsMapper, LineItem> implements LineItemService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    public LineItemsMapper lineItemsMapper;

    @Override
    public CommonResponse<List<LineItem>> GetLineItemsByOrderId(String orderId) {
        QueryWrapper<LineItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",orderId);
        List<LineItem> lineItems = lineItemsMapper.selectList(queryWrapper);
        if (lineItems.size()>0){
            return CommonResponse.BackForSuccess(lineItems);
        }else {
            return CommonResponse.BackForError("该订单不存在");
        }
    }

    @Override
    public CommonResponse<LineItem> PostLineItem(LineItem lineItem) {
        int result = lineItemsMapper.insert(lineItem);
        System.out.println(result);
        if (result ==1){
            return CommonResponse.BackForSuccess("新增lineitem成功");
        }else{
            return CommonResponse.BackForError("新增lineitem失败");
        }
    }
}
